package chapter7.section4;

import chapter7.section3.InsufficientFundsException;

//程序7-9：一个增加了取款异常的Account类
public class Account {
    private final String id; // 用户唯一的id
    private final String name;// 用户名称
    private int balance;// 当前余额
    private String state;//账户的状态，例如当为loss时表示挂失

    public Account(String id, String name, int balance) {
        super();
        this.id = id;
        this.name = name;
        this.balance = balance;
    }

    public static void main(String[] args) {
        Account acc = new Account("001", "徐嘉怡", 100);
        try {
            acc.withDraw(150);
        } catch (InsufficientFundsException e) {
            System.out.println("账户余额不足，只剩下：" + acc.balance);
            System.out.println(e.getMessage());
        }
    }

    public void withDraw(int amount) throws InsufficientFundsException {
        if (amount > this.balance) {
            throw new InsufficientFundsException(this.balance);
        } else {
            this.balance -= amount;
        }
    }
    //其他有关方法，如属性的getter和setter方法请自行添加
}
